﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Controls.Controls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.IO;
using System.Windows;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{ 
    public class MainViewModel : ViewModelBase
    { 
       
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetProd
        {
            get;
            set;
        }
        public SETINTEXDB SetData
        {
            get;
            set;
        }
        public bool IsMaster
        {
            get
            {
                return app.IsMaster;
            }
        } 
        private string _Pattern; public string Pattern { get { return _Pattern; } set { _Pattern = value; OnPropertyChanged("Pattern", value); } }
        private string _VKPNUM; public string VKPNUM { get { return _VKPNUM; } set { _VKPNUM = value; OnPropertyChanged("VKPNUM", value); } }
        private string _KRS; public string KRS { get { return _KRS; } set { _KRS = value; OnPropertyChanged("KRS", value); } }
        private string _LFD; public string LFD { get { return _LFD; } set { _LFD = value; OnPropertyChanged("LFD", value); } }
        private string _POS; public string POS { get { return _POS; } set { _POS = value; OnPropertyChanged("POS", value); } }
        private double _TARGETQTY; public double TARGETQTY { get { return _TARGETQTY; } set { _TARGETQTY = value; OnPropertyChanged("TARGETQTY", value); } }
        private string _SPINLOT; public string SPINLOT { get { return _SPINLOT; } set { _SPINLOT = value; OnPropertyChanged("SPINLOT", value); } }
        private string _GLLO; public string GLLO { get { return _GLLO; } set { _GLLO = value; OnPropertyChanged("GLLO", value); } }
        private string _GLLB; public string GLLB { get { return _GLLB; } set { _GLLB = value; OnPropertyChanged("GLLB", value); } }
        private string _GLLP; public string GLLP { get { return _GLLP; } set { _GLLP = value; OnPropertyChanged("GLLP", value); } }
        private string _CPLO; public string CPLO { get { return _CPLO; } set { _CPLO = value; OnPropertyChanged("CPLO", value); } }
        private string _CPLB; public string CPLB { get { return _CPLB; } set { _CPLB = value; OnPropertyChanged("CPLB", value); } }
        private string _CPLP; public string CPLP { get { return _CPLP; } set { _CPLP = value; OnPropertyChanged("CPLP", value); } }
        private string _DATES; public string DATES { get { return _DATES; } set { _DATES = value; OnPropertyChanged("DATES", value); } }
        private string _DATET; public string DATET { get { return _DATET; } set { _DATET = value; OnPropertyChanged("DATET", value); } }
        private long _MAXORD; public long MAXORD { get { return _MAXORD; } set { _MAXORD = value; OnPropertyChanged("MAXORD", value); } }
     
        public MainViewModel()
            : base()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetData == null)
            {
                SetData = new SETINTEXDB();
            }
            if (SetProd == null)
            {
                SetProd = new SETINTEXDB();
            }
            SQLiteDatabase = new SQLiteDatabase(dbConnection);
            string sql = "SELECT* FROM ICC_MAXORD";
            DataTable TBResult = SQLiteDatabase.GetDataTable(sql);
            foreach (DataRow row in TBResult.Rows)
            {
                MAXORD = Convert.ToInt64(row["MAXORD"].ToString());
            }
            
        }
        ~MainViewModel()
        {

        }
        public  void CheckDirectory()
        { 
            string sql = "";
           
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data\YGRCPGB04"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data\YGRCPGB04");
            }

            string SOURCE = app.ApplicationPath + "/Report/YGR/YGRCPGB04.s3db";
            if (File.Exists(dbConnection))
            {
                SQLiteDatabase = new SQLiteDatabase(dbConnection);
                string date = "", year = "", month = "", day = "";
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                string edate = Helpers.Helper.GetDateAddDays(date, -7);
                sql = "DELETE FROM ICC_GB1109 WHERE NEWDATE<'" + edate + "'";
                SQLiteDatabase.ExecuteNonQuery(sql);
                return;
            }
            if (!File.Exists(SOURCE))
            {
                MessageBox.Show("请检查文件" + app.company + ".s3db");
                return;
            }
            File.Copy(SOURCE, dbConnection);
            SQLiteDatabase = new SQLiteDatabase(dbConnection);
            DataRow dwData = SetEnv.ICC_GB1109.NewRow();
            dwData["TERMNO"] = TERMNO;
            DataSet SResult = INTEXDBC.QueryICC_GB1109(dwData, 3);
            MAXORD = 1;
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string max = row["MAXORD"].ToString();
                if (max.Trim() == "")
                    break;
                MAXORD = Convert.ToInt64(max.Replace(app.TERMNO.Trim() + ".", ""));
            }
            
            sql = "UPDATE ICC_MAXORD SET MAXORD= '" + MAXORD + "'";
            SQLiteDatabase.ExecuteNonQuery(sql);
            sql = "DELETE FROM ICC_GB1109";
            SQLiteDatabase.ExecuteNonQuery(sql);
        }
        VGLLG VGLLG = new VGLLG();
        public void LoadGLLG(DataRow dwInput)
        {
            DataSet SResult;
            try
            {
                SResult = VGLLG.QueryGLLG(dwInput, 4);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    if (row["JP05"].ToString().Trim() == "PV")
                    {
                        row["JP05"] = Helpers.Helper.ConvertGB2312To8859P1("生产留位"); 
                    }
                }
                var qData = from XD in SResult.Tables[0].AsEnumerable()
                            group XD by new
                          {
                              GLLO = XD["GLLO"].ToString(),
                              GLLB = XD["GLLB"].ToString(),
                              SPPART = XD["GPSPPART"].ToString(),
                          }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    GLBSTNET = g.Sum(XD => Convert.ToDouble(XD["GLBSTNET"].ToString())),
                                    GLBSTKG = g.Sum(XD => Convert.ToDouble(XD["GLBSTKG"].ToString())),
                                     GLBSTKA = g.Sum(XD => Convert.ToDouble(XD["GLBSTKA"].ToString())),
                                     GLBSTSP = g.Sum(XD => Convert.ToDouble(XD["GLBSTSP"].ToString())), 
                                };
                double MAX = 0;
                Pattern="";
                foreach (var XD in qData)
                {
                    string SPPART = (string)XD.KEY.SPPART;
                    double GLBSTNET = Helpers.Helper.RoundX(0.001 * XD.GLBSTNET, 1);
                    double GLBSTKG = Helpers.Helper.RoundX(0.001 * XD.GLBSTKG, 1);
                    double GLBSTKA = Helpers.Helper.RoundX(XD.GLBSTKA, 1);
                    double GLBSTSP = Helpers.Helper.RoundX(XD.GLBSTSP, 1);
                    Pattern += SPPART + ":" + GLBSTKA + "箱," + GLBSTKG + "KG" + Environment.NewLine;
                } 
                SetEnv.Clear();
                SetEnv.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AllocatGLLG(DataRow dwGLLG, string vkpnum)
        {
            try
            {
                if (dwGLLG == null)
                    return;
                if (vkpnum == null)
                    return;
                if (vkpnum == "")
                    return;
                dwGLLG["JP04"] = vkpnum;
                dwGLLG["JP05"] = Helper.ConvertGB2312To8859P1("本次留位");
                double qty = Convert.ToDouble(dwGLLG["TARGETQTY"].ToString()) / 1000;
                foreach (DataRow dwVKP in SetData.VKPOS.Rows)
                {
                    if (dwVKP["VKPNUM"].ToString() != vkpnum) continue;
                    double VKPMENGE1 = Convert.ToDouble(dwVKP["VKPMENGE1"].ToString()) / 100;
                    double VKPRESERV = Convert.ToDouble(dwVKP["VKPRESERV"].ToString()) / 100;
                    double VKPZUGET = Convert.ToDouble(dwVKP["VKPZUGET"].ToString()) / 100;
                    double VKPOFFEN = Convert.ToDouble(dwVKP["VKPOFFEN"].ToString()) / 100;
                    dwVKP["VKPZUGET"] = (VKPZUGET + qty) * 100;
                    dwVKP["VKPOFFEN"] = (VKPOFFEN - qty) * 100;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void RemoveAllocatGLLG(DataRow dwGLLG, string vkpnum, double qty)
        {
            try
            {
                if (dwGLLG == null)
                    return;
                if (vkpnum == null)
                    return;
                if (vkpnum == "")
                    return; 
                if(Helper.GetGB2312Value(dwGLLG["JP05"].ToString()).TrimEnd()=="自由")
                    return;
                double GLBSTKG = Convert.ToDouble(dwGLLG["GLBSTKG"].ToString()) / 1000;
                string allocationtyp = Helper.GetGB2312Value(dwGLLG["JP05"].ToString());
                if (allocationtyp.TrimEnd() == "生产留位")
                {
                    qty = GLBSTKG;
                }
                dwGLLG["PROCESSED"] = "N";
                dwGLLG["JP04"] = "";
                dwGLLG["JP05"] = Helpers.Helper.ConvertGB2312To8859P1("自由");
                foreach (DataRow row in SetData.VKPOS.Rows)
                {
                    if (row["VKPNUM"].ToString() != vkpnum) continue;
                    double VKPMENGE1 = Convert.ToDouble(row["VKPMENGE1"].ToString()) / 100;
                    double VKPRESERV = Convert.ToDouble(row["VKPRESERV"].ToString()) / 100;
                    double VKPZUGET = Convert.ToDouble(row["VKPZUGET"].ToString()) / 100;
                    double VKPOFFEN = Convert.ToDouble(row["VKPOFFEN"].ToString()) / 100;
                    row["VKPZUGET"] = (VKPZUGET - qty) * 100;
                    row["VKPOFFEN"] = (VKPOFFEN + qty) * 100;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
